<template>
  <a-drawer
    title="招生目录录入"
    :width="windowWidth"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="recruitEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="['ssyxm', { rules: [{ required: true, message: '请选择院系' }] }]"  @change="yxChange" disabled>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zy', { rules: [{ required: true, message: '请选择专业' }] }]"  @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['yjfxm', { rules: [{ required: true, message: '请选择研究方向' }] }]"  @change="fxChange">
                <a-select-option v-for="i in selectMenu.fx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['dsxxId', { rules: [{ required: true, message: '请选择导师' }] }]"  >
                <a-select-option v-for="i in selectMenu.ds" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="招生人数">
              <a-input placeholder="请输入..."  v-decorator="['zsrs', { rules: [{ required: true, message: '请输入招生人数' }] }]"/>
            </a-form-item>
          </a-col>
          <a-col :lg="{span:12}" :sm="{span:12}"  >
            <a-form-item label="排序">
              <a-input placeholder="请输入..." v-decorator="['seq', { rules: [{ required: false, message: '请输入招生人数' }] }]"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:24}"  >
            <a-form-item label="考试科目">
              <a-textarea placeholder="请输入..."  v-decorator="['kskm', { rules: [{ required: true, message: '请输入考试科目' }] }]" :rows="4"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:24}"  >
            <a-form-item label="备注">
              <a-textarea placeholder="请输入..."  v-decorator="['note']" :rows="4"/>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {STable} from '~'
  import {mapState} from 'vuex'

  const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  name: 'RecruitAdd',
  props: {
    recruitEditVisiable: {
      default: false
    },
    info: {
      default: () => []
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      windowWidth:"450",
      loading: false,
      postParams: {},
      selectMenu: {
        yx: [], // 院系
        zy: [],
        fx: [],
        ds: []
      },
      doctoralAdmissionYear:''
    }
  },
   beforeCreate(){
    this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  mounted(){
     this.windowWidth = document.body.clientWidth>450?450:document.body.clientWidth
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  watch:{
    info(val){
       this.form.resetFields()
      this.postParams={
        dsxxId:val.dsxxId,
        kskm:val.kskm,
        note:val.note,
        ssyxm:val.ssyxm,
        yjfxm:val.yjfxm,
        zsrs:val.zsrs,
        zy:val.zyh,
        zsxn:val.zsxn,
        id:val.id,
        seq:val.seq
      }
      this.form.setFieldsValue({'seq':val.seq})
       this.form.getFieldDecorator('zy', {initialValue: val.zyh})
      for(var key in this.info){
        if(key=="dsxxId" || key =="kskm" || key=="note" || key == "ssyxm" || key == "yjfxm" || key == "zsrs" || key =="zsxn" || key == "id"){
        this.form.getFieldDecorator(key, {initialValue: this.info[key]})
        }
        }
         this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
        let params={
        ssyxm:this.postParams.ssyxm,
        sszym:this.postParams.zy,
        xwccm:3,
        zsxn:this.doctoralAdmissionYear
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      let param={
        yjfxm:this.postParams.yjfxm,
        zym:this.postParams.zy,
        zsxn:this.postParams.zsxn,
        ssyxm:this.postParams.ssyxm
      }
      this.$api.base.getGuide(param).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      })
      this.$api.base.majorAll({lsdwh: this.postParams.ssyxm}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })

    }
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })


    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code) {
      this.form.setFieldsValue({'zy':''})
      this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxId':''})
      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
       this.$api.base.majorAll({lsdwh: code,zycc:'3'}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
    zyChange (code) {
      this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxId':''})
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        ssyxm:this.form.getFieldsValue().ssyxm,
        sszym:code,
        xwccm:3,
        zsxn:this.doctoralAdmissionYear
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      }
    },
    //选择方向
    fxChange(code){
      this.form.setFieldsValue({'dsxxId':''})
      this.form.setFieldsValue({'kskm':''})
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        yjfxm:code,
        zym:this.form.getFieldsValue().zy,
        zsxn:this.doctoralAdmissionYear,
        ssyxm:this.form.getFieldsValue().ssyxm
      }
      this.$api.base.getGuide(params).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      let param={
        ssyxm:this.form.getFieldsValue().ssyxm,
        yjfxm:code,
        sszym:this.form.getFieldsValue().zy,
      }
      this.$api.base.getKskm(param).then(res=>{
         this.form.setFieldsValue({'kskm':res.data})
      })
      }
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {

      this.form.validateFields( (err,values) => {

        if(err){
         this.$message.error("请选择必填项")
        }else{
      this.$api.doctorRecruit
        .officeUpate( Object.assign({id:this.postParams.id},this.form.getFieldsValue()))
        .then(res => {
          // this.$message.success(res.msg)
          this.$emit("success");
        })
        .finally(() => {
          this.loadingBtn = false;
           this.form.resetFields()
        });
        }
      })
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
